/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-09-04 10:31:06
 * @LastEditors: turingchang
 * @LastEditTime: 2020-09-04 11:13:35
 */

module.exports = app => {
    const DataTypes = app.Sequelize;

    const Model = app.model.define('role_permission', {
            id: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
            },
            role_id: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                default: '0',
                get() {
                    var value = this.getDataValue('role_id');
                    if (app.utils.validator.isNull(value)) {
                        value = '0';
                    }
                    return value;
                },
            },
            permission_id: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                default: '0',
                get() {
                    var value = this.getDataValue('permission_id');
                    if (app.utils.validator.isNull(value)) {
                        value = '0';
                    }
                    return value;
                },
            },
            create_time: {
                type: DataTypes.INTEGER(10),
                allowNull: false,
                //defaultValue:app.utils.time.getTimeSpan(),
                defaultValue: Math.round(new Date().getTime() / 1000).toString(),
                get() {
                    var value = this.getDataValue('create_time');
                    if (app.utils.validator.isNull(value)) {
                        value = 0;
                    }
                    return value;
                }
            },
            update_time: {
                type: DataTypes.INTEGER(10),
                allowNull: false,
                defaultValue: Math.round(new Date().getTime() / 1000).toString(),
                get() {
                    var value = this.getDataValue('update_time');
                    if (app.utils.validator.isNull(value)) {
                        value = 0;
                    }
                    return value;
                }
            },
            delete_time: {
                type: DataTypes.INTEGER(10),
                allowNull: false,
                defaultValue: '0',
                get() {
                    var value = this.getDataValue('delete_time');
                    if (app.utils.validator.isNull(value)) {
                        value = 0;
                    }
                    return value;
                }
            },
        }, {
            tableName: 'yfb_admin_role_permission',
            createdAt: "create_time",
            updatedAt: 'update_time',
            deletedAt: 'delete_time',
            timestamps: true,
            paranoid: true,
        }

    );
    Model.associate = function () {
        // app.model.Admin.RolePermission.hasMany(app.model.Admin.Role);
        // app.model.Admin.RolePermission.hasMany(app.model.Admin.Permission);
        // app.model.Admin.RolePermission.belongsToMany(app.model.Admin.Role);
    }

    return Model;
}